package cn.zf233.springsecurityjwt.config.handler;

import cn.zf233.springsecurityjwt.common.R;
import cn.zf233.springsecurityjwt.util.ServletUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by zf233 on 2021/7/12
 */
@Slf4j
@Component
public class JwtAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException {
        String uri = request.getRequestURI();
        log.info("请求访问：{}，权限不足，无法访问系统资源", uri);
        ServletUtil.renderString(response, R.createByErrorMessage("权限不足，无法访问资源:" + uri));
    }
}
